package Tree;

import Stack.*;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class BST<E extends Comparable<E>> {

    private class Node{
        private E e;
        private Node left;
        private Node right;

        public Node(E e){
            this(e,null,null);
        }

        public Node(E e,Node left,Node right){
            this.e = e;
            this.left = left;
            this.right = right;
        }

        @Override
        public String toString() {
            return e.toString();
        }
    }

    private Node root;
    private int size;

    public BST(){
        root = null;
        size = 0;
    }

    public BST(E e){
        root = new Node(e);
        size = 0;
    }

    public int getSize(){
        return size;
    }

    public boolean isEmpty(){
        return size == 0;
    }

    //向二分搜索树添加元素
    public void add(E e){
        if (root == null) {
            root = new Node(e);
            size++;
        }else
            add(root,e);
    }

    //递归调用
//    private void add(Node node,E e){
//        if (e.equals(node.e))
//            return;
//        else if(e.compareTo(node.e) < 0 && node.left == null){
//            node.left = new Node(e);
//            size ++;
//            return;
//        }else if(e.compareTo(node.e) > 0 && node.right == null){
//            node.right = new Node(e);
//            size ++;
//            return;
//        }
//
//        if (e.compareTo(node.e) < 0) {
//            add(node.left,e);
//        } else
//            add(node.right,e);
//        return;
//    }

    public Node add(Node node,E e){
        if (node == null) {
            size++;
           return new Node(e);
        }
        if (e.compareTo(node.e) < 0)
            node.left = add(node.left,e);
        else if(e.compareTo(node.e) > 0)
            node.right = add(node.right,e);
        return node;
    }

    public boolean contains(E e){
        return contains(root,e);
    }

    private Boolean contains(Node node,E e){
        if (node == null) {
            return false;
        }
        if (e.compareTo(node.e) < 0) {
            return contains(node.left,e);
        }else if(e.compareTo(node.e) > 0){
            return contains(node.right,e);
        }else
            return true;

    }
    //二分搜索树的前序遍历
    public void preOrder(){
        preOrder(root);
    }

    //前序遍历 递归算法
    private void preOrder(Node node){
        if (node == null) {
            return;
        }
        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }

    //
    public void preOrderNr(){
        Stack<Node> nodeStack = new ArrayStack<Node>();
        nodeStack.push(root);
        while (!nodeStack.isEmpty()) {
            Node cur = nodeStack.pop();
            System.out.println(cur.e);
            if (cur.right != null) {
                nodeStack.push(cur.right);
            }
            if (cur.left != null) {
                nodeStack.push(cur.left);
            }
        }
    }

    public void inOrder(){
        inOrder(root);
    }

    private void inOrder(Node node){
        if (node == null) {
            return;
        }
        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }

    public void postOrder(){
        postOrder(root);
    }

    private void postOrder(Node node){
        if (node == null) {
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.e);
    }

    public void levelOrder(){
        Queue<Node> queue= new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            Node cur = queue.remove();
            System.out.println(cur.e);
            if (cur.left != null) {
                queue.add(cur.left);
            }
            if (cur.right != null) {
                queue.add(cur.right);
            }
        }
    }

    public E minimal(){
        if (isEmpty()) {
            throw new RuntimeException("size = 0");
        }
        return getMini(root).e;
    }

    private Node getMini(Node node){
        if (node.left == null) {
            return node;
        }
        return getMini(node.left);
    }

    public E max(){
        if (isEmpty()) {
            throw new RuntimeException("size = 0");
        }
        return getMax(root).e;
    }

    private Node getMax(Node node){
        if (node.right == null) {
            return node;
        }
        return getMax(node.right);
    }

    public E removeMin(){
        E minimal = minimal();
        removeMin(root);
        return minimal;
    }

    //删除以Node为根的二分搜索树中的最小节点
    //返回删除节点后新的二分搜索树的根
    private Node removeMin(Node node){
        if (node.left == null) {
            Node rightNode = node.right;
            node.right = null;
            size--;
            //返回新的根,不必在乎是否为null
            return rightNode;
        }
        node.left = removeMin(node.left);
        return node;
    }

    public E removeMax(){
        E max = max();
        removeMax(root);
        return max;
    }

    //删除以Node为根的二分搜索树中的最小节点
    //返回删除节点后新的二分搜索树的根
    private Node removeMax(Node node){
        if (node.right == null) {
            Node leftNode = node.left;
            node.left = null;
            size--;
            //返回新的根,不必在乎是否为null
            return leftNode;
        }
        node.right = removeMax(node.right);
        return node;
    }

    public void remove(E e){
        if (!contains(e)) {
            throw new RuntimeException("元素不存在");
        }
        remove(root,e);
    }

    private Node remove(Node node,E e){
        if (node == null) {
            return null;
        }
        if (e.compareTo(node.e) < 0) {
            node.left = remove(node.left,e);
            return node;
        }else if(e.compareTo(node.e) > 0){
            node.right =  remove(node.right,e);
            return node;
        }else{
            if (node.left == null) {
                Node rightNode = node.right;
                node.right = null;
                size--;
                return rightNode;
            }else if(node.right== null){
                Node leftNode = node.left;
                node.left = null;
                size--;
                return leftNode;
            }else{
                //左子树右子树都存在,找出以该节点为根节点的右子树的最小值作为替换
                Node successor = getMini(node.right);
                successor.right = removeMin(node.right);
                successor.left = node.left;
                node.left = node.right = null;
                return successor;
            }

        }
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        generateBSTString(root,0,sb);
        return  sb.toString();
    }

    private void generateBSTString(Node node,int depth,StringBuilder sb){
        if (node == null) {
            sb.append(generateDepthString(depth)+"null\n");
            return;
        }
        sb.append(generateDepthString(depth)+node.e+"\n");
        generateBSTString(node.left,depth+1,sb);
        generateBSTString(node.right,depth+1,sb);
    }

    private String generateDepthString(int depth){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            sb.append("---");
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1,2,6,5,9,3,7,10};
        BST<Integer> tree = new BST<Integer>(5);
        for (int i = 0; i < arr.length; i++) {
            tree.add(arr[i]);
        }
        System.out.println(tree.contains(2));
        System.out.println(tree.contains(4));
        System.out.println(tree.contains(5));
        System.out.println(tree.contains(8));
        System.out.println("-----前序遍历------");
        tree.preOrder();
        System.out.println(tree);
        System.out.println("-----中序遍历------");
        tree.inOrder();
        System.out.println("-----后序遍历------");
        tree.postOrder();
        //非递归前序遍历
        System.out.println("-----非递归前序遍历------");
        tree.preOrderNr();
        //层序遍历
        System.out.println("-----层序遍历------");
        tree.levelOrder();
        System.out.println("-----树中的最小值------");
        System.out.println(tree.minimal());
        System.out.println("-----树中的最大值------");
        System.out.println(tree.max());
        System.out.println("-----删除树中的最小值------");
        System.out.println(tree.removeMin());
        System.out.println(tree);
        System.out.println("-----删除树中的最大值------");
        System.out.println(tree.removeMax());
        System.out.println(tree);
        System.out.println("-----请输入一个参数------");
        Scanner scanner = new Scanner(System.in);
        Integer i = scanner.nextInt();
        System.out.println("-----删除树中的i:"+i);
        tree.remove(i);
        System.out.println(tree);
    }

}
